/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.rotaryTable.rest;

import co.yixiang.dozer.service.IGenerator;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.rotaryTable.domain.RotaryTable;
import co.yixiang.modules.rotaryTable.service.RotaryTableService;
import co.yixiang.modules.rotaryTable.service.dto.RotaryTableDto;
import co.yixiang.modules.rotaryTable.service.dto.RotaryTableQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

/**
* @author hupeng
* @date 2020-12-23
*/
@AllArgsConstructor
@Api(tags = "RotaryTable管理")
@RestController
@RequestMapping("/api/rotaryTable")
public class RotaryTableController {

    private final RotaryTableService rotaryTableService;
    private final IGenerator generator;


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','rotaryTable:list')")
    public void download(HttpServletResponse response, RotaryTableQueryCriteria criteria) throws IOException {
        rotaryTableService.download(generator.convert(rotaryTableService.queryAll(criteria), RotaryTableDto.class), response);
    }

    @GetMapping
    @Log("查询RotaryTable")
    @ApiOperation("查询RotaryTable")
    @PreAuthorize("@el.check('admin','rotaryTable:list')")
    public ResponseEntity<Object> getRotaryTables(RotaryTableQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(rotaryTableService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增RotaryTable")
    @ApiOperation("新增RotaryTable")
    @PreAuthorize("@el.check('admin','rotaryTable:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody RotaryTable resources){
        return new ResponseEntity<>(rotaryTableService.save(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改RotaryTable")
    @ApiOperation("修改RotaryTable")
    @PreAuthorize("@el.check('admin','rotaryTable:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody RotaryTable resources){
        rotaryTableService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除RotaryTable")
    @ApiOperation("删除RotaryTable")
    @PreAuthorize("@el.check('admin','rotaryTable:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        Arrays.asList(ids).forEach(id->{
            rotaryTableService.removeById(id);
        });
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
